<?php 
use \PsiFramework\Classes\Util\UI\Printer;
use \PsiFramework\Classes\Util\UI\HTML;
use \PsiFramework\Classes\Factory\DAOFactory;
use \PsiFramework\Classes\Util\Object\Date;

$entriesArray = $response->getAttribute('entriesArray');
$alone = $response->getAttribute('alone');
?>
<article id="cont-princ" class="nocols">
    <h1><?php Printer::display($languageManager->getText('SERVICES')); ?></h1>
    <div id="monPanier" class="fiche">
        <?php
        if (count($entriesArray)==0) {
            Printer::display($languageManager->getText('NO_SERVICES'));
        } else {
        ?>
        <form
            id="servicesForm"
            method="POST"
            action="<?php Printer::display(HTML::linkHref('action=AddServicesToBasket')); ?>">
            <table>
                <thead class="tableTete">
					<tr>
						<th class="col1 tableTitre"><?php Printer::display($languageManager->getText('PRODUCT')); ?></th>
						<th class="col2 tableTitre"><?php Printer::display($languageManager->getText('SUBSCRIPTION_LENGTH')); ?></th>
						<th class="col3 tableTitre"><?php Printer::display($languageManager->getText('EXPIRATION_DATE')); ?></th>
						<th class="col4 tableTitre"><?php Printer::display($languageManager->getText('ACTIVE')); ?></th>
					</tr>
				</thead>
                <tbody>
                    <tr>
    					<td class="sep" colspan="4"></td>
    				</tr>
                    <?php
                    foreach ($entriesArray as $index=>$entryArray) {
                        
                        $date = $entryArray['responseId']->getExpirationDate();
                        $className = '';
                        if(!is_null($date)) {
                            $now = Date::now();
                            if ($now->after($date)) {
                                $className = 'expired';
                            } else if ($now->after($date->nextWeek())) {
                                $className = 'soon_to_expire';
                            } else {
                                $className = 'ordered';
                            }
                        } else {
                            $className = 'not_ordered';
                        }
                    ?>
                    <tr class="<?php Printer::display($className); ?>">
                        <td class="col1 tableTexte">
                        <?php
                        echo $entryArray['link'];
                        
                        if(!$alone){
                        ?>
                            <br/>
                            <sub><?php echo $entryArray['sites'];?></sub>
                        <?php 
                        }
                        ?>
                        </td>
                        <td class="col2 tableTexte">
                            <input
                                type="hidden"
                                name="responseId_<?php echo $index; ?>"
                                value="<?php Printer::display($entryArray['responseId']->getId()); ?>" />
                            <select name="quantity_<?php echo $index; ?>" class="serviceQuantity">
                                <option value=""></option>
                                <?php
                                foreach($entryArray['quantities'] as $q=>$label){
                                ?>
                                <option value="<?php Printer::display($q); ?>">
                                <?php
                                    echo $label;
                                ?>
                                </option>
                            <?php    
                                }
                            ?>
                            </select>
                        </td>
                        <td class="col3 tableTexte"><?php Printer::display($date); ?></td>
                        <td class="col4 tableTexte">
                            <span class="<?php Printer::display($entryArray['active']); ?>"></span>
                        </td>
                    </tr>
                    <tr>
    					<td class="sep" colspan="5"></td>
    				</tr>
                    <?php
                    }
                    ?>
                </tbody>
            </table>
            <fieldset class="formConfirm">
                <input type="hidden" name="countServices" value="<?php echo count($entriesArray); ?>" />
				<div class="actionConfirm">
					<input
                    type="submit"
                    value="<?php Printer::display($languageManager->getText('SUBSCRIBE')); ?>">
				</div>
			</fieldset>
        </form>
        <?php 
        }
        ?>
    </div>
</article>
<script>
    $(document).ready(function(){
        $("#servicesForm").submit(function(){
            var atLeastOneChecked = false;
        	
        	$(".serviceQuantity option:selected").each(function(index) {
                if ($(this).val() != '') {
                	atLeastOneChecked = true;
                }
        	}); 

            if (!atLeastOneChecked) {
                alert("<?php Printer::display($languageManager->getText('SELECT_AT_LEAST_ONE')); ?>");
            }
            
            return atLeastOneChecked;
        });
    });
</script>